package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mdd.common.entity.team.Team;
import com.mdd.common.mapper.member.MemberMapper;
import com.mdd.common.mapper.team.TeamMapper;
import com.mdd.front.service.ITeamService;
import com.mdd.front.validate.team.TeamAuthValicate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeamServiceImpl implements ITeamService {

    @Resource
    TeamMapper teamMapper;

    @Resource
    MemberMapper memberMapper;

    @Override
    public void adminApply(TeamAuthValicate valicate) {
        List<Team> team1 = teamMapper.selectList(new LambdaQueryWrapper<Team>().eq(Team::getOwnerId, valicate.getOwnerId()));
        if(CollectionUtils.isNotEmpty(team1)&&team1.size()>0){

        }else{
            Team team = new Team();
            BeanUtils.copyProperties(valicate,team);
            if(CollectionUtils.isNotEmpty(valicate.getLists())){
                team.setAttachment(valicate.getLists().stream().map(map -> map.get("url"))
                        .filter(url -> url != null &&!url.isEmpty())
                        .collect(Collectors.joining(",")));
            }
            team.setStatus(0);
            team.setOrgName(team.getName());
            team.setCreateTime(System.currentTimeMillis() / 1000);
            team.setUpdateTime(System.currentTimeMillis() / 1000);
            teamMapper.insert(team);
        }
    }

    @Override
    public Team detail(Integer userId) {
        return teamMapper.selectOne(new LambdaUpdateWrapper<Team>().eq(Team::getOwnerId,userId));
    }
}
